package com.cos.students.bo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cos.students.common.Const;
import com.cos.students.dao.RcAdminMapper;
import com.cos.students.dao.RcStudentMapper;
import com.cos.students.dao.RcTeacherMapper;
import com.cos.students.entity.RcAdmin;
import com.cos.students.entity.RcStudent;
import com.cos.students.entity.RcTeacher;
import com.cos.students.vo.input.LoginInputVo;
import com.cos.students.vo.output.LoginInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: COS
 * @time: 2022/10/25 11:39
 * @description:
 */
@Service
public class LoginBO {
    @Autowired
    private RcStudentMapper studentMapper;
    @Autowired
    private RcTeacherMapper teacherMapper;
    @Autowired
    private RcAdminMapper adminMapper;
    public LoginInfoVo getUserByName(LoginInputVo inputVo){
        LoginInfoVo infoVo=null;
        if (inputVo.getType() == Const.UserType.STUDENT) {
            RcStudent student_number = studentMapper.selectOne(
                    new QueryWrapper<RcStudent>().eq("student_number", inputVo.getUsername()));
            // 查询到数据
            if (student_number != null) {
                //数据封装到LoginInfoVo 方便后续业务处理
                infoVo = new LoginInfoVo(student_number.getStudentId(),
                        student_number.getStudentName(),
                        student_number.getStudentPassword(),Const.UserType.STUDENT);
            }
        }else if (inputVo.getType() == Const.UserType.TEACHER) {
            RcTeacher teacher_number = teacherMapper.selectOne(
                    new QueryWrapper<RcTeacher>().eq("teacher_number", inputVo.getUsername()));
            // 查询到数据
            if (teacher_number != null) {
                //数据封装到LoginInfoVo 方便后续业务处理
                infoVo = new LoginInfoVo(teacher_number.getTeacherId(),
                        teacher_number.getTeacherName(),
                        teacher_number.getTeacherPassword(),Const.UserType.TEACHER);
            }
        }else if (inputVo.getType() == Const.UserType.ADMIN) {
            RcAdmin admin_number = adminMapper.selectOne(
                    new QueryWrapper<RcAdmin>().eq("admin_username", inputVo.getUsername()));
            // 查询到数据
            if (admin_number != null) {
                //数据封装到LoginInfoVo 方便后续业务处理
                infoVo = new LoginInfoVo(admin_number.getAdminId(),
                        admin_number.getAdminUsername(),
                        admin_number.getAdminPassword(),Const.UserType.ADMIN);
            }
        }
        return infoVo;
    }
}
